<template>
  <div ref="chartRef" style="width: 100%; height: 400px" />
</template>
<script setup>
import { ref, onMounted } from "vue";
import * as echarts from "echarts";
const chartRef = ref(null);

const colors = ["#5470C6", "#91CC75", "#EE6666"];
const option = {
  color: colors,
  tooltip: {
    trigger: "axis",
    axisPointer: {
      type: "cross"
    }
  },
  grid: {
    left: "200"
  },
  legend: {
    data: ["Evaporation", "Precipitation", "Temperature"]
  },
  xAxis: [
    {
      type: "category",
      axisTick: {
        alignWithLabel: true
      },
      // prettier-ignore
      data: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
    }
  ],
  yAxis: [
    {
      type: "value",
      name: "Evaporation",
      position: "left",
      alignTicks: true,
      offset: 120,
      axisLine: {
        show: true,
        lineStyle: {
          color: colors[0]
        }
      },
      axisLabel: {
        formatter: "{value} ml"
      }
    },
    {
      type: "value",
      name: "Precipitation",
      position: "left",
      alignTicks: true,
      offset: 60,
      axisLine: {
        show: true,
        lineStyle: {
          color: colors[1]
        }
      },
      axisLabel: {
        formatter: "{value} ml"
      }
    },
    {
      type: "value",
      name: "Temperature22",
      position: "left",
      alignTicks: true,
      offset: 0,
      axisLine: {
        show: true,
        lineStyle: {
          color: colors[2]
        }
      },
      axisLabel: {
        formatter: "{value} °C"
      }
    }
  ],
  series: [
    {
      name: "Evaporation",
      type: "line",
      yAxisIndex: 0,
      data: [2.0, 4.9, 7.0, 23.2, 25.6, 76.7, 135.6, 162.2, 32.6, 20.0, 6.4, 3.3]
    },
    {
      name: "Precipitation",
      type: "line",
      yAxisIndex: 1,
      data: [2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 175.6, 182.2, 48.7, 18.8, 6.0, 2.3]
    },
    {
      name: "Temperature",
      type: "line",
      yAxisIndex: 2,
      data: [2.0, 2.2, 3.3, 4.5, 6.3, 10.2, 20.3, 23.4, 23.0, 16.5, 12.0, 6.2]
    }
  ]
};
const createChart = () => {
  console.log("create chart---");
  if (chartRef.value) {
    const myChart = echarts.init(chartRef.value);
    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);
  }
};

defineExpose({
  createChart
});
onMounted(() => {
  console.log("onMounted::");
  // if (chartContainer.value) {
  //   // chartInstance = echarts.init(chartContainer.value);
  //   // console.log("chartInstance::", chartInstance);
  //   // chartInstance.setOption(option);

  // }
});
</script>
<style lang="scss" scoped>
// .chart-content {
//   min-width: 500px;
//   min-height: 500px;
//   border: 1px solid #666;
// }
</style>
